package com.yc;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @program: concurrent
 * @description: 这是一个telnet的客户端，用于访问baidu
 *    telnet www.baidu.com 80
 *    再输入
 *    GET / HTTP/1.0
 * @author: zy
 * @create: 2022-01-15 08:54
 */
public class Test7_telnet_baidu {
    public static void main(String[] args) throws IOException {
        String website = "www.baidu.com";
        int port = 80;


        //String http = "GET / HTTP/1.0\r\nHost: www.baidu.com\r\n\r\n";  //应用层的协议
        String http="GET / HTTP/1.0\r\n\r\n";   //应用层协议
        //  http服务器 －＞   ｈｔｔｐ协议
        try (Socket s = new Socket(website, port);
             OutputStream oos = s.getOutputStream();
             InputStream iis = s.getInputStream()
        ) {
            oos.write(http.getBytes());
            oos.flush();
            ByteArrayOutputStream   baos=new ByteArrayOutputStream(  );
            byte[] bs = new byte[10 * 1024];
            int length = -1;
            while (   (length = iis.read(bs, 0, bs.length)) != -1) {
                //先把此次读取到的数据通过baos存到   内存中
                baos.write(bs, 0, length);
            }
            baos.flush();

            byte [] bb=baos.toByteArray();
            String str=new String(bb );
            System.out.println( str   );



        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}
